//
//  ManOutfitViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/15.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "ManOutfitViewController.h"//男人装
#import "ScienceTechnologyCollectionViewCell.h"//cell
#import "ScienceTechnologyModel.h"
#import "ScienceTSubViewController.h"//点击内容调转子界面阅读
@interface ManOutfitViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *scieceModelArr;//存储model数组的数组
@property (nonatomic, strong) ScienceTechnologyCollectionViewCell *cell;
@property (nonatomic, strong) NSMutableArray *modelArr;//存放 model
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) ScienceTSubViewController *scienceTSubVc;
@property (nonatomic, copy) NSString *headTitle;//跳转标题
@property (nonatomic, copy) NSString *auther_name;
@property (nonatomic, copy) NSString *full_url;//路径
@property (nonatomic, strong) ScienceTechnologyModel *model;
@property (nonatomic, copy) NSString *next_url;//前一天的路径
@property (nonatomic, strong) NSMutableArray *sectionArr;//分区
@property (nonatomic, strong) UILabel *pageNum;//页数
@end

@implementation ManOutfitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"男人装";
    self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.tabBarController.tabBar.hidden = YES;
    
    UIBarButtonItem *leftBarBut = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"weibo_back@2x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonAction)];
    self.navigationItem.leftBarButtonItem = leftBarBut;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//设置横向滑动
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.navigationController.navigationBar.translucent = YES;
    [self.view addSubview:_collectionView];
    //改变导航栏字体颜色和大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kP6width(20)],NSForegroundColorAttributeName:[UIColor purpleColor]}];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight-kP6Height(50), kWidth, kP6Height(50))];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
   
    self.pageNum = [[UILabel alloc] initWithFrame:CGRectMake(kP6width(100), kP6Height(15), kP6width(175), kP6width(20))];
    self.pageNum.textAlignment = NSTextAlignmentCenter;
    self.pageNum.font = [UIFont systemFontOfSize:kP6width(16)];
    [view addSubview:self.pageNum];
    self.pageNum.textColor = [UIColor purpleColor];
  
    //解析数据
    self.scieceModelArr = [ACHCommon scienceModelAsynchronousURL:kManOutfitUrl collectionVIew:self.collectionView];
    self.sectionArr = [NSMutableArray array];
    [self.sectionArr addObject:self.scieceModelArr];
}

#pragma mark - 实现 collectionView 协议方法
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;//行间距
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidth, kHeight);//布局
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.sectionArr[section] count];//个数
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.sectionArr.count;
}
//返回 item 内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *str = @"cell";
    [collectionView registerClass:[ScienceTechnologyCollectionViewCell class] forCellWithReuseIdentifier:str];
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    self.model = [[ScienceTechnologyModel alloc] init];
    //NSMutableArray *modelArray = self.sectionArr[indexPath.section];
    self.modelArr = self.sectionArr[indexPath.section][indexPath.row];
    [self labelWithCell:_cell model:_model arr:_modelArr];
    UITapGestureRecognizer *tapImageView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageViewAction:)];
    [self.cell.headImageView addGestureRecognizer:tapImageView];
    
    [self.cell.oneBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.oneBut.tag = 100;
    [self.cell.twoBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.twoBut.tag = 101;
    [self.cell.threeBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.threeBut.tag = 102;
    [self.cell.fourBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.fourBut.tag = 103;
    [self.cell.fiverBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.fiverBut.tag = 104;

#pragma mark - 当前页/总页数
    NSInteger count = indexPath.section*6+indexPath.row+1;
    NSInteger  numAll = self.sectionArr.count * 6;
    self.pageNum.text = [NSString stringWithFormat:@"%ld/%ld",count,numAll];
    return self.cell;
}
#pragma mark - 调整 标题图片尺寸 
//加载 cell
- (void)labelWithCell:(ScienceTechnologyCollectionViewCell *)cell model:(ScienceTechnologyModel *)model arr:(NSMutableArray *)modelArr
{
    NSMutableArray *myArr = [NSMutableArray arrayWithArray:modelArr];
    int p = 1;
    model = myArr[p];
    if (model.thumbnail_pic.length != 0) {
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:model.thumbnail_pic]];
        cell.headLabel.text = model.title;
    }
    else
    {
        while (model.url.length == 0) {
            p++;
            if (p++>=myArr.count) {
                p = 1;
                break;
            }
            model = myArr[p];
        }
        model = myArr[p];
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:model.url]];
        cell.headLabel.text = model.title;
    }
    [myArr removeObjectAtIndex:p];
    for (int i =0; i < myArr.count; i++) {
        switch (i) {
            case 0:
                model = myArr[0];
                [self modelTitleLabel:cell.oneTitlelLable name:cell.oneAuther_name model:model];
                break;
            case 1:
                model = myArr[1];
                [self modelTitleLabel:cell.twoTitleLabel name:cell.twoAuther_name model:model];
                break;
            case 2:
                model = myArr[2];
                [self modelTitleLabel:cell.threeTitleLable name:cell.threeAuther_name model:model];
                break;
            case 3:
                model = myArr[3];
                [self modelTitleLabel:cell.fourTitleLable name:cell.fourAuther_name model:model];
                break;
            case 4:
                model = myArr[4];
                [self modelTitleLabel:cell.fiveTitleLable name:cell.fiveAuther_name model:model];
                break;
            default:
                break;
        }
    }
    self.next_url = model.next_url;
}
- (void)modelTitleLabel:(UILabel *)label name:(UILabel *)name model:(ScienceTechnologyModel *)model
{
    label.text = model.title;
    NSString *data = model.date;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSDate *time = [df dateFromString:data];
    NSDate *cureeTime = [NSDate date];
    NSTimeInterval timeInterVal = [time timeIntervalSinceDate:cureeTime]/3600;//小时
    if (timeInterVal/-24 < 1) {
        name.text = model.auther_name;
    }
    else
    {
        int day =(int)timeInterVal/-24;
        name.text = [NSString stringWithFormat:@"%@ %d天前",model.auther_name,day];
    }
    name.textColor = [UIColor grayColor];
    
}

#pragma mark- 给标题图片加轻拍手势, 点击并跳转
- (void)tapImageViewAction:(ScienceTechnologyModel *)model
{
    self.scienceTSubVc = [[ScienceTSubViewController alloc] init];
    NSMutableArray *myArr = [NSMutableArray arrayWithArray:_modelArr];
    int p = 1;
    model = myArr[p];
    while (model.url.length < 4) {
        p++;
        if (p++>=myArr.count) {
            p = 1;
            break;
        }
        model = myArr[p];
    }
    model = myArr[p];
    [self wihtModel:model];
    [self.navigationController pushViewController:_scienceTSubVc animated:YES];
}

- (void)buttonAction:(UIButton *)button
{
    self.scienceTSubVc = [[ScienceTSubViewController alloc] init];
    NSMutableArray *myArr = [NSMutableArray arrayWithArray:_modelArr];
    int p = 1;
    self.model = myArr[p];
    while (self.model.url.length < 1) {
        p++;
        if (p++>=myArr.count) {
            p = 1;
            break;
        }
        self.model = myArr[p];
    }
    [myArr removeObjectAtIndex:p];
    switch (button.tag) {
        case 100:
            self.model = myArr[0];
            [self wihtModel:_model];
            break;
        case 101:
            self.model = myArr[1];
            [self wihtModel:_model];
            break;
        case 102:
            self.model = myArr[2];
            [self wihtModel:_model];
            break;
        case 103:
            self.model = myArr[3];
            [self wihtModel:_model];
            break;
        case 104:
            self.model = myArr[4];
            [self wihtModel:_model];
            break;
        default:
            break;
    }
    [self.navigationController pushViewController:_scienceTSubVc animated:YES];
}

- (void)wihtModel:(ScienceTechnologyModel *)model
{
    self.scienceTSubVc.headTitle = model.title;
    self.scienceTSubVc.auther_name = model.auther_name;
    self.scienceTSubVc.full_url = model.full_url;
     self.scienceTSubVc.weburl = model.weburl;
}

#pragma mark - 当滑动完之后再次解析新的数椐
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    ScienceTechnologyCollectionViewCell *cell =[[self.collectionView visibleCells] firstObject];
    NSIndexPath *path = [self.collectionView indexPathForCell:cell];
    if ((path.row +1)%4 == 0 && (path.section+1) == self.sectionArr.count) {
        NSMutableArray *newModelArr = [ACHCommon scienceModelAsynchronousURL:self.next_url collectionVIew:self.collectionView];
        [self.sectionArr addObject:newModelArr];
    }
}

- (void)leftBarButtonAction
{
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
